package com.go.ssm.web.interceptor;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.go.ssm.web.exception.BaseRuntimeException;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

public class ExceptionInterceptor extends AbstractInterceptor {
	private static final long serialVersionUID = 1L;
	private final Logger log = LoggerFactory.getLogger(this.getClass());

	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		try {
			return invocation.invoke();
		} catch (Exception e) {
			if (e instanceof BaseRuntimeException) {
				BaseRuntimeException be = (BaseRuntimeException) e;
				// log.error(be.getErrorCode(), be);
				// return Action.ERROR;
				invocation.getInvocationContext()
						.put("info", be.getErrorCode());
				log.info("fail in action:"
						+ invocation.getProxy().getActionName(), e);
				return "info";
			} else if (e instanceof RuntimeException) {
				RuntimeException re = (RuntimeException) e;
				log.error("unkown error code", re);
				return Action.ERROR;
			} else if (e instanceof Exception) {
				log.error("fail in action:"
						+ invocation.getProxy().getActionName(), e);
				return Action.ERROR;
			}
			return Action.ERROR;

			/*
			 * catch (BOException be) { log.error(be.getMessage(), be); return
			 * Action.ERROR; } catch (InfoException e) {
			 * invocation.getInvocationContext().put("info", e.getMessage());
			 * log.info("fail in action:" +
			 * invocation.getProxy().getActionName(), e); return "info"; }catch
			 * (Exception e) { log.error( "fail in action:" +
			 * invocation.getProxy().getActionName(), e); return Action.ERROR; }
			 */
		}
	}
}